<?php
// 应用公共文件
function pointInPolygon($point, $polygon)
{
    $x        = $point[0];
    $y        = $point[1];
    $isInside = false;
    $count    = count($polygon);
    if ($count < 3) {
        return false;
    }

    for ($i = 0, $j = $count - 1; $i < $count; $j = $i++) {
        if ((($polygon[$i][1] > $y) != ($polygon[$j][1] > $y)) &&
            ($x < ($polygon[$j][0] - $polygon[$i][0]) * ($y - $polygon[$i][1]) / ($polygon[$j][1] - $polygon[$i][1]) + $polygon[$i][0])) {
            $isInside = !$isInside;
        }
    }

    return $isInside;
}

function bcround($number, $scale = 0)
{
    if ($scale < 0) $scale = 0;
    $sign = '';
    if (bccomp('0', $number, 64) == 1) $sign = '-';
    $increment = $sign . '0.' . str_repeat('0', $scale) . '5';
    $number    = bcadd($number, $increment, $scale + 1);
    return bcadd($number, '0', $scale);
}

// 解析用户ip
/**
 * @param $num
 * @return bool
 */
function parseIpAddress($ip)
{
    $ip2region = new \Ip2Region();
    $geo       = $ip2region->btreeSearch($ip);
    $res       = explode('|', str_replace(['0|'], '', $geo['region'] ?? ''));
    array_pop($res);
    return $res;
}